﻿using Common.Attributes;
using MODEL;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.Admin
{
   public  class  RecruitmentController : Controller
    {
        #region 校园招聘会列表视图 + ActionResult index()
        /// <summary>
        /// 校园招聘会列表视图
        /// </summary>
        /// <returns></returns>
        public ActionResult index()
        {
            return View();
        } 
        #endregion

        #region 获取招聘会列表数据 +GetRecruitmentData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取招聘会列表数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetRecruitmentData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            var list = OperateContext.Current.BLLSession.Iqgzx_recruit_noticeBLL.GetPagedList(pageIndex, pageSize, x => x.Id != null, m => m.Id).Select(x => x.ToPOCO());
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_recruit_noticeBLL.GetListBy(x => x.Id != null).Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载修改学生 window 的html + EditRecruitment()
        /// <summary>
        /// 加载修改学生 window 的html
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditRecruitment(int id)
        {
            //SetDropDonwList();
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_recruit_noticeBLL.GetListBy(x => x.Id == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改学生信息 EditRecruitment(MODEL.qgzx_recruit_notice model)
        /// <summary>
        ///post修改校园招聘会信息 EditRecruitment(MODEL.qgzx_recruit_notice model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditRecruitment(MODEL.qgzx_recruit_notice model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_recruit_noticeBLL.Modify(model, "Id", "Period", "RecSubject", "RecTime", "RecAddress", "IsOpen" );
            if (res > 0)
            {
                return Redirect("/admin/Recruitment/index");
            }
            return Redirect("/admin/Recruitment/index");
        }
        #endregion

        #region 在这新增校园招聘会 html代码 + addRecruitment()
        /// <summary>
        /// 在这新增校园招聘会 html代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult addRecruitment()
        {
            //SetDropDonwList();
            return PartialView("EditRecruitment");
        }
        #endregion

        #region 在这新增校园招聘会 html代码 + addRecruitment(MODEL.qgzx_recruit_notice model)
        /// <summary>
        /// 在这新增校园招聘会 html代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult addRecruitment(MODEL.qgzx_recruit_notice model)
        {

            int res = OperateContext.Current.BLLSession.Iqgzx_recruit_noticeBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/admin/Recruitment/index?ok");
            }
            return Redirect("/admin/Recruitment/index?err");
        }
        #endregion

        #region 删除校园招聘会信息 + delRecruitment()
        /// <summary>
        /// 删除学生 + DelRecruitment()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult delRecruitment()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_recruit_noticeBLL.DelBy(x => x.Id == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限"+ex, null, "");
            }
        }
        #endregion

        #region 查看这一届招聘会岗位信息 + ActionResult JoinRecPosition

        [HttpGet]
        /// <summary>
        /// 查看这一届招聘会岗位信息
        /// </summary>
        /// <param name="id">Recruitment id</param>
        /// <returns></returns>

        public ActionResult JoinRecPosition()
        {
            int RecPosiId = int.Parse(Request.QueryString["Id"]);
            return View();
        }
        #endregion

        #region  查看这一届招聘会岗位信息 + ActionResult JoinRecPosition
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        ///  查看这一届招聘会岗位信息
        /// </summary>
        /// <returns></returns>
        public ActionResult JoinRecPosition(FormCollection form)
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //获取父权限id
            int RecruitmentId = int.Parse(Request.QueryString["Id"]);

            
            var list = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetPagedList(pageIndex, pageSize, x => x.RecruitmentId == RecruitmentId, m => m.PositionId);
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_positionBLL.GetListBy(x => x.PositionId != null).Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 高级搜索(比较笨，先用这个方法) + ActionResult Search()
        /// <summary>
        /// 高级搜索(比较笨，先用这个方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            string Id = Request.QueryString["Id"];
            string Period = Request.QueryString["Period"];
            string RecSubject = Request.QueryString["RecSubject"];
            string RecTime = Request.QueryString["RecTime"];
            string RecAddress = Request.QueryString["RecAddress"];
            string IsOpen = Request.QueryString["IsOpen"];
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！
            var list = OperateContext.Current.BLLSession.Iqgzx_recruit_noticeBLL.GetListBy(x => x.Id != null).ToList();
            if (Period != "")
            {
                list = list.Where(x => x.Period.Contains(Period)).ToList();
            }
            if ( RecAddress!= "")
            {
                list = list.Where(x => x.RecAddress.Contains(RecAddress)).ToList();
            }
            if (RecTime != "")
            {
                list = list.Where(x => x.RecTime.ToString().Contains(RecTime)).ToList();
            }
            if (IsOpen != "")
            {
                list = list.Where(x => x.IsOpen.ToString().Contains(IsOpen)).ToList();
            }
            
            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion
    }
    
}
